﻿using AutoMapper;
using LianChuDemo.IDal.BaseInfoManagerDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WenDangOne.Common.Lib.TypeTools;
using WenDangOne.DbModel.BaseInfoManagerDal;
using WenDangOne.ViewModel.BaseInfoManagerView;

namespace LianChuDemo.SqlServerDal.BaseInfoManagerDal
{
    //这实际上是一个接口，只有把所有的接口都实现了就不会报错
    public class ModuleDbDal : BaseDal, IModuleDbDal
    {
        //私有的

        private IMapper _mapper;
        //公有的
        public ModuleDbDal(SystemCenterDbContext systemCenterDbContext,IMapper mapper) : base(systemCenterDbContext)
        {
            _mapper = mapper;
        }
        public List<ModuleDb> GetModuleDbsAll() 
        {
            return _systemCenterDbContext.ModuleDb.ToList();
        }
        //获取模块   根据code来查出来，在接口层也要调用
        public ModuleDb GetModuleDbByModuleCode(string moduleCode)
        {
            //根据这个条件查询一条数据，验证唯一的方法验证唯一的方法
            return _systemCenterDbContext.ModuleDb.FirstOrDefault(c => c.module_code == moduleCode);
        }

        //添加
        public bool ModuleAdd(ModuleDb moduleDb)
        {
            _systemCenterDbContext.ModuleDb.Add(moduleDb);
            return _systemCenterDbContext.SaveChanges() > 0;
        }
        //反填，我们先尝试用ling左连接查询
        public ModuleDbView GetModuleDbViewByCode(string moduleCode)
        {
            var view = (from dm in _systemCenterDbContext.ModuleDb
                        join pm in _systemCenterDbContext.ModuleDb
                        on dm.parent_code equals pm.module_code into leftJoinPdm
                        from leftJoinParent in leftJoinPdm.DefaultIfEmpty()//假设有null值，则返回null
                        where dm.module_code == moduleCode
                        select new
                        {
                            pname = leftJoinParent.module_name,
                            mdb = dm
                        }).FirstOrDefault();
            if (view == null) return null;
            ModuleDbView mview = _mapper.Map<ModuleDbView>(view.mdb);
            mview.parent_name = view.pname;
            return mview;
             
        }
        //反填,使用子查询来实现
        public ModuleDbView GetModuleDbViewByCodeSonSql(string moduleCode)
        {
            var mdb = _systemCenterDbContext.ModuleDb.FirstOrDefault(p => p.module_code == moduleCode);
            if (mdb == null) return null;
            ModuleDbView mview = _mapper.Map<ModuleDbView>(mdb);
            var p = _systemCenterDbContext.ModuleDb.FirstOrDefault(c => c.module_code == mdb.parent_code);
            mview.parent_name = p != null ? p.module_name : "";
            return mview;
        }
        //修改
        public bool ModuleUpdate(ModuleDb moduleDb)
        {
            ModuleDb db = _systemCenterDbContext.ModuleDb.FirstOrDefault(c => c.module_code == moduleDb.module_code);
            ModelCopier.CopyModel(moduleDb, db, "creator_name", "creator_date");
            return _systemCenterDbContext.SaveChanges() > 0;
        }

        //根据用户名获取用户下的所有模块
        public List<ModuleDb> GetModuleDbsByUserName(string userName)
        {
            //三表联查
            return (from m in _systemCenterDbContext.ModuleDb
                    join rmr in _systemCenterDbContext.RoleModuleRelation 
                    on m.module_code equals rmr.module_code
                    join aurr in _systemCenterDbContext.AdminUserRoleRelation 
                    on rmr.role_id equals aurr.role_id
                    where aurr.user_name == userName
                    select m
                    ).ToList();
        }
    }
}
